home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Libraries / GammaPasLib 1.1 / gammaPas.c < prev    next >
Encoding:
Text File  |  1993-07-20  |  7.0 KB  |  215 lines  |  [TEXT/KAHL]

  1. // File "gamma.c" - Source for Altering the Gamma Tables of GDevices
  2. //   Last updated 3/13/93, MJS
  3.  
  4. // * ****************************************************************************** *
  5. //
  6. //    This is the Source Code for the Gamma Utils Library file. Use this to build
  7. //        new functionality into the library or make an A4-based library. 
  8. //    See the header file "gamma.h" for much more information. -- MJS
  9. //
  10. // * ****************************************************************************** *
  11.  
  12. #include <GestaltEqu.h>
  13. #include <Quickdraw.h>
  14. #include <Video.h>
  15. #include <Traps.h>
  16. #include "gammaPas.h"
  17.  
  18. long            gammaUtilsInstalled;
  19. globalGammasHdl    gammaTables;
  20.  
  21. // * ****************************************************************************** *
  22. // * ****************************************************************************** *
  23.  
  24. pascal Boolean IsGammaAvailable() {
  25.     GDHandle theGDevice;
  26.  
  27.     if (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) ==
  28.             NGetTrapAddress(_Unimplemented, ToolTrap)) return(0);
  29.     
  30.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice))
  31.         if (TestDeviceAttribute(theGDevice, screenDevice) && 
  32.                 TestDeviceAttribute(theGDevice, noDriver)) return(0);
  33.  
  34.     return(-1);
  35.     }
  36.  
  37. // * ****************************************************************************** *
  38. // * ****************************************************************************** *
  39.  
  40. pascal Boolean IsOneGammaAvailable(GDHandle theGDevice) {
  41.     
  42.     if (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) ==
  43.             NGetTrapAddress(_Unimplemented, ToolTrap)) return(0);
  44.     
  45.     if (TestDeviceAttribute(theGDevice, screenDevice) && 
  46.             TestDeviceAttribute(theGDevice, noDriver)) return(0);
  47.     
  48.     return(-1);
  49.     }
  50.  
  51. // * ****************************************************************************** *
  52. // * ****************************************************************************** *
  53.  
  54. pascal OSErr SetupGammaTools() {
  55.     short errorCold=0;
  56.     globalGammasHdl tempHdl;
  57.     GammaTblPtr    masterGTable;
  58.     GDHandle theGDevice;
  59.  
  60.     if (gammaUtilsInstalled == kGammaUtilsSig) return(-1);
  61.     
  62.     gammaTables = 0;
  63.     gammaUtilsInstalled = kGammaUtilsSig;
  64.     for(theGDevice = GetDeviceList(); theGDevice; theGDevice = GetNextDevice(theGDevice)) {
  65.         if (errorCold = GetDevGammaTable(theGDevice, &masterGTable)) return(errorCold);
  66.         
  67.         tempHdl = (globalGammasHdl) NewHandle(sizeof(globalGammas));
  68.         if (tempHdl == 0) return(errorCold = MemError());
  69.         
  70.         (*tempHdl)->size = sizeof(GammaTbl) + masterGTable->gFormulaSize +
  71.                 (masterGTable->gChanCnt * masterGTable->gDataCnt * masterGTable->gDataWidth / 8);
  72.         (*tempHdl)->dataOffset = masterGTable->gFormulaSize;
  73.         (*tempHdl)->theGDevice = theGDevice;
  74.         
  75.         (*tempHdl)->saved = (GammaTblHandle) NewHandle((*tempHdl)->size);
  76.         if ((*tempHdl)->saved == 0) return(errorCold = MemError());
  77.         (*tempHdl)->hacked = (GammaTblHandle) NewHandle((*tempHdl)->size);
  78.         if ((*tempHdl)->hacked == 0) return(errorCold = MemError());
  79.     
  80.         BlockMove((Ptr) masterGTable, (Ptr) *(*tempHdl)->saved, (*tempHdl)->size);
  81.         
  82.         (*tempHdl)->next = gammaTables;
  83.         gammaTables = tempHdl;
  84.         }
  85.  
  86.     return(0);
  87.     }
  88.  
  89. // * ****************************************************************************** *
  90. // * ****************************************************************************** *
  91.  
  92. pascal OSErr DoGammaFade(short percent) {
  93.     short errorCold=0;
  94.     register long size, i, theNum;
  95.     globalGammasHdl tempHdl;
  96.     unsigned char *dataPtr;
  97.  
  98.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  99.     
  100.     for(tempHdl = gammaTables; tempHdl; tempHdl = (*tempHdl)->next) {
  101.     
  102.         BlockMove((Ptr) *(*tempHdl)->saved, (Ptr) *(*tempHdl)->hacked, (*tempHdl)->size);
  103.         dataPtr = (unsigned char *) (*(*tempHdl)->hacked)->gFormulaData + (*tempHdl)->dataOffset;
  104.         size = (*(*tempHdl)->hacked)->gChanCnt * (*(*tempHdl)->hacked)->gDataCnt;
  105.         
  106.         for(i=0; i < size; i++) {
  107.             theNum = dataPtr[i];
  108.             theNum = (theNum * percent) / 100;
  109.             dataPtr[i] = theNum;
  110.             }
  111.         
  112.         if (errorCold = SetDevGammaTable((*tempHdl)->theGDevice, (*tempHdl)->hacked))
  113.             return(errorCold);
  114.         }
  115.         
  116.     return(0);
  117.     }
  118.  
  119. // * ****************************************************************************** *
  120. // * ****************************************************************************** *
  121.  
  122. pascal OSErr DoOneGammaFade(GDHandle theGDevice, short percent) {
  123.     short errorCold=0;
  124.     register long size, i, theNum;
  125.     globalGammasHdl tempHdl;
  126.     unsigned char *dataPtr;
  127.  
  128.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  129.     for(tempHdl = gammaTables; tempHdl && (theGDevice != (*tempHdl)->theGDevice);
  130.             tempHdl = (*tempHdl)->next);
  131.         
  132.     BlockMove((Ptr) *(*tempHdl)->saved, (Ptr) *(*tempHdl)->hacked, (*tempHdl)->size);
  133.     dataPtr = (unsigned char *) (*(*tempHdl)->hacked)->gFormulaData + (*tempHdl)->dataOffset;
  134.     size = (*(*tempHdl)->hacked)->gChanCnt * (*(*tempHdl)->hacked)->gDataCnt;
  135.     
  136.     for(i=0; i < size; i++) {
  137.         theNum = dataPtr[i];
  138.         theNum = (theNum * percent) / 100;
  139.         dataPtr[i] = theNum;
  140.         }
  141.     
  142.     errorCold = SetDevGammaTable((*tempHdl)->theGDevice, (*tempHdl)->hacked);
  143.     
  144.     return(errorCold);
  145.     }
  146.  
  147. // * ****************************************************************************** *
  148. // * ****************************************************************************** *
  149.  
  150. pascal OSErr DisposeGammaTools() {
  151.     globalGammasHdl tempHdl, nextHdl;
  152.  
  153.     if (gammaUtilsInstalled != kGammaUtilsSig) return(-1); 
  154.     for(tempHdl = gammaTables; tempHdl; tempHdl = nextHdl) {
  155.         nextHdl = (*tempHdl)->next;
  156.         DisposeHandle((Handle) (*tempHdl)->saved);
  157.         DisposeHandle((Handle) (*tempHdl)->hacked);
  158.         DisposeHandle((Handle) tempHdl);
  159.         }
  160.         
  161.     gammaUtilsInstalled = 0;
  162.     return(0);
  163.     }
  164.  
  165. // * ****************************************************************************** *
  166. // * ****************************************************************************** *
  167.  
  168. pascal OSErr GetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  169.     short errorCold=0;
  170.     CntrlParam  *myCPB;
  171.  
  172.     ((long *) theTable)[0] = 0;
  173.  
  174.     if (IsOneGammaAvailable(theGDevice) == 0) return(-1);
  175.             
  176.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  177.     myCPB->csCode = cscGetGamma;
  178.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  179.     *(GammaTblPtr **) myCPB->csParam = theTable;
  180.     errorCold = PBStatus((ParmBlkPtr) myCPB, 0);
  181.  
  182.     DisposePtr((Ptr) myCPB);
  183.     return(errorCold);
  184.     }
  185.  
  186. // * ****************************************************************************** *
  187. // * ****************************************************************************** *
  188.  
  189. pascal OSErr SetDevGammaTable(GDHandle theGDevice, GammaTblPtr *theTable) {
  190.     CntrlParam *myCPB;
  191.     short errorCold=0;
  192.     CTabHandle cTab;
  193.     GDHandle saveGDevice;
  194.  
  195.     if (IsOneGammaAvailable(theGDevice) == 0) return(-1);
  196.  
  197.     if ((myCPB = (CntrlParam *) NewPtrClear(sizeof(CntrlParam))) == 0) return(MemError());
  198.     myCPB->csCode = cscSetGamma;
  199.     myCPB->ioCRefNum = (*theGDevice)->gdRefNum;
  200.     **(GammaTblPtr **) myCPB->csParam = *theTable;
  201.  
  202.     errorCold = PBControl((ParmBlkPtr) myCPB, 0);
  203.  
  204.     if (errorCold == 0) {
  205.         saveGDevice = GetGDevice();
  206.         SetGDevice(theGDevice);
  207.          cTab = (*(*theGDevice)->gdPMap)->pmTable;
  208.         SetEntries (0, (*cTab)->ctSize, (*cTab)->ctTable);
  209.         SetGDevice(saveGDevice);
  210.         }
  211.  
  212.     DisposePtr((Ptr) myCPB);
  213.     return (errorCold);
  214.     }
  215.